//
//  AddNoteController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "AddNoteController.h"

#import "FormattingUtilities.h"
#import "GSSCommunicator.h"

@implementation AddNoteController

@synthesize folder;

@synthesize navItem;
@synthesize nameTextField;
@synthesize textView;

- (id)initWithFolder:(GSSObject *)aFolder {
    if (self = [super initWithNibName:@"AddNoteController" bundle:nil]) {
        folder = [aFolder retain];
    }
    return self;
}

- (void)dealloc {
    [folder release];
    
    [navItem release];
    [nameTextField release];
    [textView release];
    
    [super dealloc];
}


//MARK: -
//MARK: UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navItem.title = LocalizedString(@"Add note");
    NSString *name = [[GSSCommunicator defaultCommunicator] generateUniqueName:@"untitled note.txt"
                                                                  withTemplate:@"untitled note %d.txt"
                                                                     forObject:self.folder];
    self.nameTextField.text = name;
    [self.textView becomeFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self
                      selector:@selector(keyboardWillShow:)
                          name:UIKeyboardWillShowNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(keyboardWillHide:)
                          name:UIKeyboardWillHideNotification
                        object:nil];    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


//MARK: -
//MARK: Notification Handlers

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardBounds = [[[notification userInfo]
                              objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGRect frame = self.textView.frame;
    frame.size.height -= keyboardBounds.size.height;
    self.textView.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *)notification {
    CGRect keyboardBounds = [[[notification userInfo]
                              objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    CGRect frame = self.textView.frame;
    frame.size.height += keyboardBounds.size.height;
    self.textView.frame = frame;
}


//MARK: -
//MARK: Button Actions

- (IBAction)savePressed {
    NSString *name = self.nameTextField.text;
    if ([name length]) {
        GSSCommunicator *communicator = [GSSCommunicator defaultCommunicator];
        NSString *uri = [folder.uri stringByAppendingString:
                         [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
        GSSObject *file = [communicator objectWithURI:uri withType:GSSObjectTypeFile];
        NSData *data = [self.textView.text dataUsingEncoding:NSUTF8StringEncoding];
        file.name = name;
        file.content = @"text/plain";
        [communicator createFile:file withData:data inFolder:folder];
    }
    
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)cancelPressed {
    [self dismissModalViewControllerAnimated:YES];
}

@end